$(function(){
    list();
    function list() {
        //先清空
        $('tbody').empty();
        var token = localStorage.getItem("token");
        $.ajax({
            url: "http://localhost:81/student",
            headers: {
                "token": token
            },
            type: "get",
            success: function (result) {
                if (result.success) {
                    //教师列表
                    var $teachers = $(result.data);
                    //遍历： 有一个对象就应该添加一行数据
                    for (var i = 0; i < $teachers.length; i++) {
                        var t = $teachers[i];
                        //组装所有的td
                        var td1 = '<th scope="row">' + (i + 1) + '</th>';
                        var td2 = "<td>" + t.name + "</td>";
                        var td4 = "<td>" + t.sex + "</td>";
                        var td6 = "<td>" + t.address + "</td>";
                        var jsonStr = JSON.stringify(t);
                        var td7 = "<td><button type='button' class='btn btn-primary btn-sm update' data-obj='" + jsonStr + "'>修改</button>&nbsp;&nbsp;"
                        + '<button type="button" class="btn btn-warning btn-sm del" data-id="' + t.id + '">删除</button></td>';
                        var tr = $("<tr></tr>").append(td1).append(td2).append(td4).append(td6).append(td7);
                        $('tbody').append(tr);
                    }
                } else if (!result.success && "401" == result.code) {
                    location.href = "login.html";
                } else {
                    alert("系统繁忙，请稍后重试");
                }
            },
            dataType: "json"
        });
    }
//    添加
   $(".add").on('click',function(){
    //每次弹出前先清除数据
    $("#form").resetForm();
    //每次弹出前先清楚隐藏id
    $("id").val("");
    //将弹出框显示出来
    $("#myModal").show();
   })
   //关闭这个弹窗
   $(".closeModal").on('click',function(){
    $("#myModal").hide();
   })
   //提交：既然是提交就要想后端发送请求
   $(".save").on('click',function(){
  var b= $("#form").serializeJson();
   $.ajax({
    contentType:"application/json",
    url:"http://localhost:81/student",
    headers: {
        "token": localStorage.getItem("token")},
     data:JSON.stringify(b),
     type:"put",
     success:function(result){
       if(result.success){
        list();
       }else if(result.code==401){
        location.href="login.html"
         }
       else{
        alert("当前繁忙请稍后重试");
       }
       $("#myModal").hide();
     },
     dataType:"json"
   });
   })
   //删除
   $("tbody").on('click','.del',function(){
      var a=confirm("你确定要删除吗？");
      if(a){
       var id= $(this).data("id");
       $.ajax({
        url:"http://localhost:81/student/"+id,
        headers: {
            "token": localStorage.getItem("token")},
        type:"delete",
        success:function(result){
            if(result.success){
            list();
        }else if(result.code==401){
            location.href="login.html"
             }
        else{
            alert("当前繁忙请稍后重试");
           }
        },
        dataType:"json"
       });
      }
   })
      // 修改
      $("tbody").on('click','.update',function(){
        var obj=$(this).data("obj");
        $("#form").setForm(obj);
        $("#myModal").show("slow");
      })
})